package com.capgemini.teamtwo.config;

import com.capgemini.teamtwo.common.BusinessException;
import com.capgemini.teamtwo.common.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.time.format.DateTimeParseException;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseBean sysException(Exception e) {
        log.error("系统异常", e);
        return ResponseBean.failed();
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResponseBean sysException(BusinessException e) {
        log.error("业务异常", e);
        return ResponseBean.failed(e.getCode(), e.getMessage(), e.getErrorData());
    }
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public ResponseBean<String> nullPointerException(NullPointerException e){
        log.error("数据缺失",e);
        return ResponseBean.failed("数据缺失");
    }

    @ExceptionHandler(DateTimeParseException.class)
    @ResponseBody
    public ResponseBean<String> dateFormatException(DateTimeParseException e){
        log.error("日期格式错误");
        return ResponseBean.failed("日期格式错误");
    }
}
